<template>
  <div class="ele-body">
    <el-card>
      <el-form label-width="100px">
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="请求方式：">
              <el-select v-model="method" placeholder="请选择请求方式" style="width: 100%">
                <el-option label="POST" value="post" />
                <el-option label="GET" value="get" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="请求路径：">
              <el-input v-model="url" placeholder="请输入请求路径：">
                <template slot="prepend">{{ $setting.baseURL }}</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item>
              <el-button type="primary" @click="onPost">查询</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <el-card>
      <div slot="header" class="clearfix">
        <span>请求参数</span>
      </div>
      <el-form ref="paramsForm" label-width="100px">
        <div v-for="item in itemNum" :key="item.id">
          <el-row :gutter="20">
            <el-col :span="8">
              <el-form-item label="参数名" prop="req">
                <el-input v-model="key[item]" />
              </el-form-item></el-col>
            <el-col :span="8">
              <el-form-item label="参数值">
                <el-input v-model="value[item]" />
              </el-form-item>
            </el-col>
            <el-col v-if="item === itemNum" :span="8">
              <el-form-item>
                <el-button icon="el-icon-circle-plus-outline" @click="addItem">添加</el-button>
                <el-button icon="el-icon-remove-outline" @click="delItem">删除</el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </div>
      </el-form>
    </el-card>
    <el-card class="response">
      <div slot="header" class="clearfix">
        <span>请求响应</span>
      </div>
      <pre>{{ response }}</pre>
    </el-card>
  </div>
</template>

<script>
import qs from 'qs'
export default {
  name: 'ToolsPost', // 请求测试页
  data() {
    return {
      method: 'get',
      url: '',
      params: {},
      key: [],
      value: [],
      itemNum: 1,
      response: ''
    }
  },
  methods: {
    /* 发送请求 */
    onPost() {
      if (this.url === '') {
        this.$message({ type: 'error', message: '请输入请求路径' })
        return
      }
      const formData = {}
      this.key.forEach((item, key) => {
        formData[item] = this.value[key]
      })
      if (this.method === 'get') {
        this.$http.get(`${this.$setting.baseURL}${this.url}?${qs.stringify(formData)}`).then(res => {
          this.response = res.data.data
        }).catch(err => {
          this.$alert(err.response.data, `${err.response.status}错误`, {
            confirmButtonText: '确定',
            customClass: 'MsgBox'
          })
        })
      } else if (this.method === 'post') {
        this.$http.post(`${this.$setting.baseURL}${this.url}`, formData).then(res => {
          this.$alert(res.data, `请求成功`, {
            confirmButtonText: '确定',
            customClass: 'MsgBox'
          })
        }).catch(err => {
          this.$alert(err.response.data, `${err.response.status}错误`, {
            confirmButtonText: '确定',
            customClass: 'MsgBox'
          })
        })
      }
    },
    /* 添加参数表单 */
    addItem() {
      this.itemNum++
    },
    /* 删除参数表单 */
    delItem() {
      this.key.splice(this.itemNum - 1, 1)
      this.value.splice(this.itemNum - 1, 1)
      this.itemNum--
    }
  }
}
</script>

<style lang="scss">
.MsgBox{
  width: 60% !important
}
</style>
